<?php
/**
*评论
**/

namespace app\admin\controller;


use app\admin\common\Base;
use think\Db;
use think\facade\Request;

class Comment extends Base
{
	use \app\admin\traits\BaseFn;//引入公共方法(增删改查)  具体看traits

    protected $model = null;

    public function initialize(){
        parent::initialize();
        $this->model = new \app\admin\model\Comment;
    }
    public function index()
    {
        if(Request::isAjax()){

            list($where,$sort) = $this->buildQuery();

            $list = $this->model
                ->where($where)
                ->order($sort)
                ->select()
                ->toArray();
            $total = count($list);
            $data = [
                "total"=>$total,
                'rows'=>$list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * 编辑
     */
    public function edit(){
        if(Request::isAjax()){
            $code = 0;
            $msg = 'success';
            $data = null;
            try{
                $where = [
                    'id'=>Request::param('id'),
                ];
                $update = Request::except('id');
                $this->model->save($update,$where);
                $msg = "保存成功";

            }catch (Exception $e){
                $msg = $e->getMessage();
            }

            return json(['data'=>$data,'msg'=>$msg,'code'=>$code]);
        }
        $where = Request::param();
        $data = $this->model->where($where)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
}